package util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class VskPage {
	public static String divClassString="";
	public static String aClassString="";
	public static String selectClassString="";
	/**
	 * 记录总数
	 */
	public static int allRecordCount=0;
	/**
	 * 每页显示记录
	 */
	private int everyPageRecord=10;
	/**
	 * 当前页
	 */
	private int currentPage=0;
	/**
	 * 总页数
	 */
	private int maxPage=5;
	/**
	 * 总记录sql语句
	 */
	private String allCountSql="";
	
	public int getAllRecordCount() {
		return allRecordCount;
	}


	public int getEveryPageRecord() {
		return everyPageRecord;
	}

	public void setEveryPageRecord(int everyPageRecord) {
		this.everyPageRecord = everyPageRecord;
	}

	public int getCurrentPage() {
		return currentPage;
	}
	
	public String getAllCountSql() {
		return allCountSql;
	}

	public void setAllCountSql(String allCountSql) {
		this.allCountSql = allCountSql;
	}
	
	public void processAllCountSql(){
		if(this.getCurrentPage()==1 && allCountSql.indexOf("top")>1)
			this.allCountSql=this.allCountSql.replace("top "+everyPageRecord+" *", "count(*)");
		else if(allCountSql.indexOf("top")<1)
			this.allCountSql=this.allCountSql.replace("*", " count(*)");
	}

	public String processPageSql(String inTableName,String inPrimaryKey){
		return getCurrentPage()<=1?"select top "+everyPageRecord+" * from "+inTableName:"select top "+everyPageRecord+" * from "+inTableName+" where "+inPrimaryKey+" not in (select top "+(currentPage-1)*everyPageRecord+" "+inPrimaryKey+" from "+inTableName+")";		
	}
	
	public String processPageSql(int inCurrentPage,String inTableName,String inPrimaryKey,String inWhere){
		return inCurrentPage<=1?"select top "+everyPageRecord+" * from "+inTableName+inWhere:"select top "+everyPageRecord+" * from "+inTableName+" where "+inPrimaryKey+" not in (select top "+(currentPage-1)*everyPageRecord+" "+inPrimaryKey+" from "+inTableName+")"+inWhere;		
	}
	/**
	 * 得到总页数
	 * @return
	 */
	public int getMaxPage(){
		return (allRecordCount/everyPageRecord)+1; 
	}
	public int getAllRecordCount(Connection inConn,String inSql){
		try{
		Statement st=inConn.createStatement();
		processAllCountSql();
		ResultSet rs=st.executeQuery(this.getAllCountSql());
		if(rs.next())
			allRecordCount=rs.getInt(1);
		}catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		this.maxPage=getMaxPage();
		return allRecordCount;
	}
	
	public String printCtrla(String inPage){
		String vskPageStringa="<div align='center' "+divClassString+">";
		vskPageStringa+="共"+allRecordCount+"条数据,当前第"+currentPage+"页 ";
		vskPageStringa+=getSpaceString(1)+"<a href='"+inPage+"?Page=1' "+aClassString+" >首页</a>";
		vskPageStringa+=getSpaceString(2)+"<a href='"+inPage+"?Page="+(currentPage-1<=1?1:currentPage-1)+"'  "+aClassString+" >上页</a>";
		vskPageStringa+=getSpaceString(2)+"<select onchange='window.location=\""+inPage+"?Page=\"+this.value;'  "+selectClassString+" >";
		vskPageStringa+="<option value='1'>请选择</option>";
		if(maxPage>1){
			for(int i=1;i<=maxPage;i++)
		    vskPageStringa+="<option value='"+i+"' "+(currentPage==i?" selected ":"")+" >"+i+"</option> \n";
		}
		vskPageStringa+="</select>";
		vskPageStringa+=getSpaceString(2)+"<a href='"+inPage+"?Page="+(currentPage+1>=maxPage?maxPage:currentPage+1)+"' "+aClassString+" >下页</a>";
		vskPageStringa+=getSpaceString(2)+"<a href='"+inPage+"?Page="+maxPage+"' "+aClassString+" >尾页</a>";
		vskPageStringa+=getSpaceString(2)+"共"+getMaxPage()+"页";
		vskPageStringa+="</div>";
		return vskPageStringa;
	}
	public String printCtrla(String inPage,String inParameter){
		String vskPageStringa="<div align='center' "+divClassString+" >";
		vskPageStringa+="共"+allRecordCount+"条数据,当前第"+currentPage+"页 ";
		vskPageStringa+=getSpaceString(1)+"<a href='"+inPage+"?Page=1"+inParameter+"' "+aClassString+" >首页</a>";
		vskPageStringa+=getSpaceString(2)+"<a href='"+inPage+"?Page="+(currentPage-1<=1?1:currentPage-1)+inParameter+"' "+aClassString+" >上页</a>";
		vskPageStringa+=getSpaceString(2)+"<select onchange='window.location=\""+inPage+"?Page=\"+this.value+\""+inParameter+"\";' "+selectClassString+" >";
		vskPageStringa+="<option value='1'>请选择</option>";
		if(maxPage>1){
			for(int i=1;i<=maxPage;i++)
		    vskPageStringa+="<option value='"+i+"' "+(currentPage==i?" selected ":"")+" >"+i+"</option> \n";
		}
		vskPageStringa+="</select>";
		vskPageStringa+=getSpaceString(2)+"<a href='"+inPage+"?Page="+(currentPage+1>=maxPage?maxPage:currentPage+1)+inParameter+"' "+aClassString+" >下页</a>";
		vskPageStringa+=getSpaceString(2)+"<a href='"+inPage+"?Page="+maxPage+inParameter+"' "+aClassString+" >尾页</a>";
		vskPageStringa+=getSpaceString(2)+"共"+getMaxPage()+"页";
		vskPageStringa+="</div>";
		return vskPageStringa;
	}
    
	public VskPage() {
	}

	public VskPage(int everyPageRecord, int currentPage) {
		this.everyPageRecord = everyPageRecord;
		this.currentPage = currentPage;
	}
	public static String getSpaceString(int inNum){
		String spaceString="";
		for(int i=0;i<inNum;i++)
			spaceString+="&nbsp;";
		return spaceString;
	}
	public static void main(String[] args) {
		String sql="select top 10 * from SmsRecordTable where SmsIndex not in (select top 20 SmsIndex from SmsRecordTable) and  PhoneNumber like '%%' and SmsContent like '%%' and Status='接收' order by SmsTime asc";
		VskPage vp=new VskPage(10,3);
		//System.out.println(vp.processPageSql(vp.getCurrentPage(), "Tb_User", "UserId", (vp.getCurrentPage()<=1?" where ":" and ")+" 1=1"));
		System.out.println(sql.indexOf("top "+vp.getEveryPageRecord()+" *"));
		
	}
	

}
